<template>
	<view>
		<u-top-tips ref="uTips"></u-top-tips>
	</view>
</template>

<script>
	import {
		isWeiXin
	} from '@/utils/index'
	import http from '@/utils/http'

	export default {
		name: 'PaySubmit',
		props: ['payment', 'successUrl'],
		data() {
			return {
				code: ''
			}
		},
		created() {

		},
		methods: {
			handlerSubmit(url, data, callback) {
				let that = this
				uni.login({
					success(resCode) {
						data.code = resCode.code
						http
							.post(url, data)
							.then(result => {
								let res = result.data
								if (res.data.pay_status == 1) {
									that.$refs.uTips.show({
										title: '支付成功',
										type: 'success'
									})
									if (typeof callback === 'function') {
										callback()
										return false
									} else {
										setTimeout(() => {
											if (that.$route.path === that.successUrl) {
												location.reload()
											} else {
												uni.navigateTo({
													url: that.successUrl
												})
											}
										}, 1500)
									}
									return false
								} else {
									that.handlerPay(res.data)
								}
							})
							.catch(() => {
								setTimeout(() => {
									if (that.$Route.path === that.successUrl) {
										location.reload()
									} else {
										uni.navigateTo({
											url: that.successUrl
										})
									}
								}, 1500)
							})
					}
				})
			},
			handlerPay(data) {
				console.log(data,'继续支付调起参数',this.payment)
				if (this.payment == 'wxpay') {
					let parameters = data.parameter.jsApiParameters
					// 调起支付
					uni.requestPayment({
						provider: this.payment,
						timeStamp: parameters.timeStamp,
						signType: parameters.signType,
						paySign: parameters.paySign,
						package: parameters.package,
						nonceStr: parameters.nonceStr,
						appId: parameters.appId,
						success: res => {
							this.$emit('handlePaySuccess', res);
						},
						fail: err => {
							this.$emit('handlePayFail', err);
						}
					});
				}
			}

		}
	}
</script>
<style lang="scss" scoped>
</style>